<template>
  <el-input v-model="input" style="left:500px;width: 240px" placeholder="Please input" @input="onInput">东方</el-input>
  <el-text>{{ transfer }}</el-text>
    
</template>
    
<script lang="ts" setup>
//一号→1号
// 十八号→18号

import { ref } from 'vue'
import type { UploadInstance, ElMessage } from 'element-plus'

const input = ref('');
const transfer = ref('');

function onInput(){
    var str = input.value;

    // const chineseReg = /(零|一|二|两|三|四|五|六|七|八|九)/g;
    // var arr = str.split(chineseReg);
    // console.log(arr);

    var arr = ["零","一","二","三","四","五","六","七","八","九","十"];
    var char;
    var startIndex = -1;
    var endIndex = -1;
    var splitArr = [];
    for(var i=0; i<str.length; i++){
        char = str.charAt(i);

        if(startIndex != -1){
            if((arr.indexOf(char) == -1 || i == str.length-1) && endIndex == -1){
                endIndex = i;
                if(i == str.length-1){
                    endIndex = i + 1;
                }
                splitArr.push(str.substring(startIndex, endIndex));
                startIndex = -1;
            }
        }

        if(arr.indexOf(char) != -1 && startIndex == -1){
            startIndex = i;
            endIndex = -1;

            if(i == str.length-1){
                endIndex = i + 1;
                splitArr.push(str.substring(startIndex, endIndex));
                startIndex = -1;
            }
        }
    }
    console.log(splitArr);

    for (i = 0; i < splitArr.length; i++) {
        var numstr = chineseToNumber(splitArr[i]) + "";
        console.log(numstr);
        str = str.replace(splitArr[i], numstr);
    }

    transfer.value = str;
}

function chineseToNumber(chineseNum:string) {
    var num:any = {
        '零': 0, '一': 1, '二': 2, '两': 2, '三': 3,
        '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9
    };
    var unit:any = {
        '十': {
            value: 10,
            secUnit: false
        },
        '百': {
            value: 100,
            secUnit: false
        },
        '千': {
            value: 1000,
            secUnit: false
        },
        '万': {
            value: 10000,
            secUnit: true
        }
    };
 
    var rtn = 0;
    var rtnSec = 0;
    var secUnit = false;
    var str = chineseNum.replace(/零([千百十])/g, '零万$1'); // 将“零千”、“零百”、“零十”转换为“零万”
    for (var i = 0; i < str.length; i++) {
        var numStr = str.charAt(i);
        var unitStr = str.substr(i + 1, 1);
        var n = num[numStr];
        var u = unit[unitStr];
        if (u) {
            if (u.secUnit) {
                if (rtn == 0) {
                    rtn = rtnSec;
                    rtnSec = 0;
                } else {
                    rtnSec = rtn;
                    rtn *= u.value;
                }
                secUnit = true;
            } else {
                if (secUnit) {
                    rtnSec += n * u.value;
                } else {
                    rtn += n * u.value;
                }
                secUnit = false;
            }
            i++;
        } else if (n) {
            if (secUnit) {
                rtnSec += n;
            } else {
                rtn += n;
            }
        }
    }
    return rtn + rtnSec;
}

</script>
    